﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;
using Models;

namespace 包覆门.KaikongManager
{
    public partial class KaikongEditing : Form
    {
        public KaikongEditing()
        {
            InitializeComponent();
        }
        public bool IsNew { get; set; }
        public string EditingId { get; set; }
        private KaikongDAL dal = new KaikongDAL();
        //保存
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!ValidateData()) {
                MessageBox.Show("请填写打孔方向");
                return;
            }
            Kaikong kk = new Kaikong();
            kk.Direction = this.tbDirection.Text.Trim();
            kk.Bzsm = this.tbBzsm.Text.Trim();
            if (IsNew)
            {
               
                if (dal.SaveData(kk) == 1)
                {
                    MessageBox.Show("保存成功");
                    DialogResult = DialogResult.OK;
                }
                else {
                    MessageBox.Show("保存失败");
                    DialogResult = DialogResult.No;
                }
            }
            else 
            {
                kk.Kid = Convert.ToInt32(EditingId);
                if(dal.UpdateData(kk)==1){
                    MessageBox.Show("保存成功");
                    DialogResult = DialogResult.OK;
                }else{
                    MessageBox.Show("保存失败");
                    DialogResult = DialogResult.No;
                }
            }
        }
        //取消
        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.No;
            Close();
        }

        private void KaikongEditing_Load(object sender, EventArgs e)
        {
            if (IsNew) { } else {
                Kaikong kk = dal.GetById(EditingId);
                this.tbDirection.Text = kk.Direction;
                this.tbBzsm.Text = kk.Bzsm;
            }
        }
        private bool ValidateData() {
            
            string direction = this.tbDirection.Text;
            if (string.IsNullOrWhiteSpace(direction))
            {
                return false;
            }
            else 
            {
                return true;
            }
        }
    }
}
